home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap13 / Bez3d / Bez3d.c next >
Encoding:
C/C++ Source or Header  |  1999-09-04  |  3.2 KB  |  137 lines

  1. // Bez3d.c
  2. // OpenGL SuperBible, Chapter 13
  3. // Demonstrates 3D Bezier Surfaces
  4. // Program by Richard S. Wright Jr.
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8. #include <gl/glu.h>
  9. #include <gl/glut.h>
  10. #include <math.h>
  11.  
  12. // The number of control points for this curve
  13. GLint nNumPoints = 3;
  14.  
  15. GLfloat ctrlPoints[3][3][3]= {{{  -4.0f, 0.0f, 4.0f},    
  16.                               { -2.0f, 4.0f, 4.0f},    
  17.                               {  4.0f, 0.0f, 4.0f }},
  18.                              
  19.                               {{  -4.0f, 0.0f, 0.0f},    
  20.                               { -2.0f, 4.0f, 0.0f},    
  21.                               {  4.0f, 0.0f, 0.0f }},
  22.                               
  23.                               {{  -4.0f, 0.0f, -4.0f},    
  24.                               { -2.0f, 4.0f, -4.0f},    
  25.                               {  4.0f, 0.0f, -4.0f }}};
  26.  
  27.  
  28. // This function is used to superimpose the control points over the curve
  29. void DrawPoints(void)
  30.     {
  31.     int i,j;    // Counting variables
  32.  
  33.     // Set point size larger to make more visible
  34.     glPointSize(5.0f);
  35.  
  36.     // Loop through all control points for this example
  37.     glBegin(GL_POINTS);
  38.         for(i = 0; i < nNumPoints; i++)
  39.             for(j = 0; j < 3; j++)
  40.                 glVertex3fv(ctrlPoints[i][j]);
  41.     glEnd();
  42.     }
  43.  
  44.  
  45.  
  46. // Called to draw scene
  47. void RenderScene(void)
  48.     {
  49.     // Clear the window with current clearing color
  50.     glClear(GL_COLOR_BUFFER_BIT);
  51.  
  52.     // Save the modelview matrix stack
  53.     glMatrixMode(GL_MODELVIEW);
  54.     glPushMatrix();
  55.  
  56.     // Rotate the mesh around to make it easier to see
  57.     glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
  58.     glRotatef(60.0f, 1.0f, 0.0f, 0.0f);
  59.  
  60.     // Sets up the Bezier
  61.     // This actually only needs to be called once and could go in
  62.     // the setup function
  63.     glMap2f(GL_MAP2_VERTEX_3,    // Type of data generated
  64.     0.0f,                        // Lower u range
  65.     10.0f,                        // Upper u range
  66.     3,                            // Distance between points in the data
  67.     3,                            // Dimension in u direction (order)
  68.     0.0f,                        // Lover v range
  69.     10.0f,                        // Upper v range
  70.     9,                            // Distance between points in the data
  71.     3,                            // Dimension in v direction (order)
  72.     &ctrlPoints[0][0][0]);        // array of control points
  73.  
  74.     // Enable the evaluator
  75.     glEnable(GL_MAP2_VERTEX_3);
  76.  
  77.     // Use higher level functions to map to a grid, then evaluate the
  78.     // entire thing.
  79.  
  80.     // Map a grid of 10 points from 0 to 10
  81.     glMapGrid2f(10,0.0f,10.0f,10,0.0f,10.0f);
  82.  
  83.     // Evaluate the grid, using lines
  84.     glEvalMesh2(GL_LINE,0,10,0,10);
  85.  
  86.     // Draw the Control Points
  87.     DrawPoints();
  88.  
  89.     // Restore the modelview matrix
  90.     glPopMatrix();
  91.  
  92.     // Dispalay the image
  93.     glutSwapBuffers();
  94.     }
  95.  
  96. // This function does any needed initialization on the rendering
  97. // context. 
  98. void SetupRC()
  99.     {
  100.     // Clear Window to white
  101.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
  102.  
  103.     // Draw in Blue
  104.     glColor3f(0.0f, 0.0f, 1.0f);    }
  105.  
  106.  
  107. void ChangeSize(int w, int h)
  108.     {
  109.     // Prevent a divide by zero
  110.     if(h == 0)
  111.         h = 1;
  112.  
  113.     // Set Viewport to window dimensions
  114.     glViewport(0, 0, w, h);
  115.     glMatrixMode(GL_PROJECTION);
  116.     glLoadIdentity();
  117.  
  118.     glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -10.0f, 10.0f);
  119.  
  120.     // Modelview matrix reset
  121.     glMatrixMode(GL_MODELVIEW);
  122.     glLoadIdentity();
  123.     }
  124.  
  125. int main(int argc, char* argv[])
  126.     {
  127.     glutInit(&argc, argv);
  128.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  129.     glutCreateWindow("3D Bezier Surface");
  130.     glutReshapeFunc(ChangeSize);
  131.     glutDisplayFunc(RenderScene);
  132.     SetupRC();
  133.     glutMainLoop();
  134.  
  135.     return 0;
  136.     }
  137.